import {Dispatch, createContext, SetStateAction, useState,  useMemo, useContext } from "react";

type UserInfo = {
    username: string;
    role: string;
    from: string;
    login_time: string;
}

type AppContentProps = {
    state: UserInfo; 
    setState: Dispatch<SetStateAction<UserInfo>>;
};

const AppContent = createContext<AppContentProps>(null!)

export const useAppContent = () => {
    return useContext(AppContent)
}

export default function AppContentProvider({children}:{children: React.ReactNode}) {
    const [state, setState] = useState<UserInfo>({
        username: "",
        role: "",
        from: "",
        login_time: ""
    })

    //缓存状态，不会随着页面刷新重新计算，除非 state 改变
    const contentValue = useMemo(() => {
        return{state, setState}}, [state])


    return <AppContent.Provider value={contentValue}>{children}</AppContent.Provider>
}